/*****************************************************
** Author: 
** Date: 2017-06-20
** Copyright(c) All Rights Reserved.
*****************************************************/

#ifndef __DNA_SPEECH_H_
#define __DNA_SPEECH_H_

#ifdef __cplusplus
extern "C"{
#endif

/***** INCLUDES *************************************/

/***** TYPEDEFS *************************************/
typedef struct dna_snd_conf_t
{
    /*
     *  @brief: sampling rate
     *          support 8KHz/16KHz
     *          please refer to DNA_SPEECH_RATE_*, AMR-NB we choose 8KHz
     */
    unsigned int        rate;
    /*
     *  @brief: Two channels (stereo), One channel (mono)
     *          NAU88C10 only support mono
     *          please refer to DNA_SPEECH_CHANNEL_*
     */
    unsigned int        channels;
    /*
     *  @brief: S8/U8
     *          S16_LE/S16_BE/U16_LE/U16_BE
     *          S24_LE/S24_BE/U24_LE/U24_BE
     *          S32_LE/S32_BE/U32_LE/U32_BE
     *          please refer to DNA_SPEECH_FORMAT_*
     *          currently we choose S16_LE
     */
    unsigned int        format;
    /*
     *  @brief: code type
     *          please refer to DNA_SPEECH_CODEC_TYPE_*
     *          currently support PCM/AMR-NB
     */
    unsigned int        codectype;
} dna_snd_conf_t;

enum
{
    DNA_SPEECH_STATUS_OK = 0,
    DNA_SPEECH_ERR_DEV,
    DNA_SPEECH_ERR_RATE,
    DNA_SPEECH_ERR_CHAN,
    DNA_SPEECH_ERR_FORMAT,
    DNA_SPEECH_ERR_PERIOD,
    DNA_SPEECH_ERR_CODEC,
    DNA_SPEECH_ERR_MEM,
    DNA_SPEECH_ERR_INVALID,
    DNA_SPEECH_ERR_DAEMON,
    DNA_SPEECH_ERR_QUEUE,
    DNA_SPEECH_ERR_I2S,
    DNA_SPEECH_ERR_I2C,
    DNA_SPEECH_ERR_VOLUME,
    DNA_SPEECH_ERR_PARA,
    DNA_SPEECH_ERR_CTL_READ,
    DNA_SPEECH_ERR_CTL_WRITE,
    DNA_SPEECH_ERR_DUMP_REG,
    DNA_SPEECH_ERR_MAX
};

enum
{
    DNA_SPEECH_RATE_8K,
    DNA_SPEECH_RATE_16K,
    DNA_SPEECH_RATE_MAX
};

enum
{
    DNA_SPEECH_CHANNEL_MONO,
    DNA_SPEECH_CHANNEL_STEREO,
    DNA_SPEECH_CHANNEL_MAX
};

enum
{
    DNA_SPEECH_FORMAT_S8,
    DNA_SPEECH_FORMAT_U8,
    DNA_SPEECH_FORMAT_S16_LE,
    DNA_SPEECH_FORMAT_S16_BE,
    DNA_SPEECH_FORMAT_U16_LE,
    DNA_SPEECH_FORMAT_U16_BE,
    DNA_SPEECH_FORMAT_S24_LE,
    DNA_SPEECH_FORMAT_S24_BE,
    DNA_SPEECH_FORMAT_U24_LE,
    DNA_SPEECH_FORMAT_U24_BE,
    DNA_SPEECH_FORMAT_S32_LE,
    DNA_SPEECH_FORMAT_S32_BE,
    DNA_SPEECH_FORMAT_U32_LE,
    DNA_SPEECH_FORMAT_U32_BE,
    DNA_SPEECH_FORMAT_MAX
};

enum
{
    DNA_SPEECH_CODEC_TYPE_PCM = 0,
    DNA_SPEECH_CODEC_TYPE_AMR_NB,
    DNA_SPEECH_CODEC_TYPE_AMR_WB,
    DNA_SPEECH_CODEC_TYPE_AAC,
    DNA_SPEECH_CODEC_TYPE_SPEEX,
    DNA_SPEECH_CODEC_TYPE_MP3,
    DNA_SPEECH_CODEC_TYPE_MAX,
};

typedef enum dna_snd_event_t
{
    DNA_SND_EVENT_NONE = 0,
    DNA_SND_EVENT_HAS_CODEC_DATA,
    DNA_SND_EVENT_FILL_CODEC_DATA,
    DNA_SND_EVENT_PLAY_COMPLETE,
    DNA_SND_EVENT_MAX
} dna_snd_event_t;

typedef struct dna_snd_event_msg_t
{
    dna_snd_event_t     event_id;
    unsigned int        len;
} dna_snd_event_msg_t;

/***** DEFINES **************************************/

enum
{
#ifdef DNA_SNDCARD_NAU88C10_INSTALL
    DNA_SNDCARD_NAU88C10,
#endif
#ifdef DNA_SNDCARD_ISD9160_INSTALL
    DNA_SNDCARD_ISD9160,
#endif
    DNA_SNDCARD_MAX,
};


#define DNA_SND_ENC_BUFFER_SIZE             (8 * 1024)

#define DNA_SND_DEC_BUFFER_SIZE             (16 * 1024)

#define DNA_SND_AUDIO_REC_BUFFER_SIZE       (8 * 1024)

#define DNA_SND_AUDIO_PLY_BUFFER_SIZE       (16 * 1024)

/*
 *  @brief: common control address will start from 0
 *          customized control address will start from 
 */
enum
{
    /*
     *  @breif: write only
     *          disable record
     */
    DNA_SPEECH_CTLADDR_MIC_MUTE,
    /*
     *  @brief: write only
     *          enable record
     */
    DNA_SPEECH_CTLADDR_MIC_UNMUTE,
    /*
     *  @breif: write only
     *          disable play
     */
    DNA_SPEECH_CTLADDR_SPK_MUTE,
    /*
     *  @brief: write only
     *          enable play
     */
    DNA_SPEECH_CTLADDR_SPK_UNMUTE,
    /*
     *  @brief: read/write
     *          support unsigned char type
     */
    DNA_SPEECH_CTLADDR_VOLUME,
    /*
     *  @brief: read only
     *          if sound card support local speech recognize, get the result from this ctladdr
     */
    DNA_SPEECH_CTLADDR_SR,
};

/***** EXTERNAL VARIABLES ***************************/

/***** EXTERNAL FUNCTIONS ***************************/

/***** VARIABLES ************************************/

extern const short sin_table_500Hz[16];

/***** PROTOTYPES ***********************************/

/***** CONSTANTS ************************************/
// sound card initialize API
int dna_speech_open(int device, dna_snd_conf_t *conf);
int dna_speech_post_init(int device);
int dna_speech_is_opened(int device);
int dna_speech_is_alive(int device);
int dna_speech_event_register(int device, void (*func)(int, dna_snd_event_msg_t *));
int dna_speech_event_deregister(int device);
int dna_speech_set_getting_threshold(int device, unsigned int threshold);
int dna_speech_set_filling_threshold(int device, unsigned int threshold);
int dna_speech_device_start(int device);
int dna_speech_device_close(int device);
// sound card audio data layer API
int dna_speech_enable_record(int device);
int dna_speech_disable_record(int device);
int dna_speech_enable_play(int device);
int dna_speech_disable_play(int device);
int dna_speech_is_recording(int device);
int dna_speech_is_playing(int device);
int dna_speech_get_rec_free_size(int device);
int dna_speech_get_rec_used_size(int device);
int dna_speech_get_enc_free_size(int device);
int dna_speech_get_enc_used_size(int device);
int dna_speech_get_ply_free_size(int device);
int dna_speech_get_ply_used_size(int device);
int dna_speech_get_dec_free_size(int device);
int dna_speech_get_dec_used_size(int device);
int dna_speech_reset_rec_buffer(int device);
int dna_speech_reset_ply_buffer(int device);
int dna_speech_read_audio(int device, unsigned char *data, int len);
int dna_speech_write_audio(int device, unsigned char *data, int len);
// sound card audio control layer API
int dna_speech_ctrlread(int device, unsigned char reg, unsigned char *data, int len);
int dna_speech_ctrlwrite(int device, unsigned char reg, unsigned char *data, int len);
int dna_speech_dump_register(int device, char *pbuf, int maxsize);
// sound card tools API
int dna_speech_mic_mute(int device);
int dna_speech_mic_unmute(int device);
int dna_speech_spk_mute(int device);
int dna_speech_spk_unmute(int device);
int dna_speech_read_volume(int device, int *volume);
int dna_speech_write_volume(int device, int volume);


#ifdef __cplusplus
}
#endif

#endif /* __DNA_SPEECH_H_ */

